﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bowerbird.Core.Dtos;
using Bowerbird.Core.Storage;
using Bowerbird.Core.Utils;
using AutoMapper;

namespace Bowerbird.Core.Services
{
    public class UserService
    {
        public UserDto LogIn(string identityProvider, string nameIdentifier, string name)
        {
            User user = UserRepository.GetByNameIdentifier(identityProvider, nameIdentifier);

            if (user == null)
            {
                user = UserRepository.Register(identityProvider, nameIdentifier, name);
            }

            return Assembler<User, UserDto>.ToDto(user);
        }

        public UserDto CompleteProfile(string id, string name, string email)
        {
            User user = RepositoryBase<User>.GetById(id);

            if (user != null)
            {
                user.Name = name;
                user.EMail = email;
                user.IsNewUser = false;

                user = RepositoryBase<User>.SaveOrUpdate(user);
            }
            else
            {
                throw new ArgumentException("User Not found for the specified id", "id");
            }

            return Assembler<User, UserDto>.ToDto(user);
        }

        public ContactDataDto AddNewContactData(string userId, int contactDataType, string name, string value, string visibleTo)
        {
            ContactData contactData = UserRepository.AddNewContactData(userId, contactDataType, name, value, visibleTo);

            return Assembler<ContactData, ContactDataDto>.ToDto(contactData);
        }

        public IEnumerable<ContactDataDto> GetContactData(string userId)
        {
            IEnumerable<ContactData> contactData = UserRepository.GetContactData(userId);

            return Assembler<ContactData, ContactDataDto>.ToDto(contactData);
        }

        public void RemoveContactData(string userId, string id)
        {
            UserRepository.RemoveContactData(userId, id);
        }

        public IEnumerable<UserDto> SuggestUsers(string userId)
        {
            var users = UserRepository.GetAll().Where(u => u.Id != userId).AsEnumerable();

            return Assembler<User, UserDto>.ToDto(users);
        }

    }
}
